package ThreadRunableVolite;

import java.util.ArrayList;
import java.util.List;

/**
 * @author macpro
 * @date 2019-07-07
 * @function 多线程卖票
 * 怎么这么简单？应该有更复杂的应用于业务
 */
public class ThreadSellTicket {

    private static int index;
    private static List<String> mTickets;

    public static void main(String[] args) {

        mTickets = new ArrayList<>();
        for (int i = 0; i < 40; i++) {
            mTickets.add(i+"");
        }

        index = 0;
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    String ticket =  getIndex();
                    if (ticket == null) {
                        break;
                    }
                    System.out.println(Thread.currentThread().getName()+"  "+ ticket);
                }
            }
        }, "sell-A").start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        Thread.sleep(70);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    String ticket =  getIndex();
                    if (ticket == null) {
                        break;
                    }
                    System.out.println(Thread.currentThread().getName()+"  "+ ticket);
                }
            }
        }, "sell-B").start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        Thread.sleep(80);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    String ticket =  getIndex();
                    if (ticket == null) {
                        break;
                    }
                    System.out.println(Thread.currentThread().getName()+"  "+ ticket);
                }
            }
        }, "sell-C").start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        Thread.sleep(90);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    String ticket =  getIndex();
                    if (ticket == null) {
                        break;
                    }
                    System.out.println(Thread.currentThread().getName()+"  "+ ticket);
                }
            }
        }, "sell-D").start();

    }

    private synchronized static String getIndex() {
        if (index>=mTickets.size()){
            return null;
        }
        return mTickets.get(index++);
    }
}
